package controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import model.Company;
import service.CompanyService;
import service.impl.CompanyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @Author: 燕子豪
 * @Date: 2023/1/2 0:35
 */
@WebServlet("/companyById")
public class CompanyByIdServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        CompanyService companyService = new CompanyServiceImpl();
        //解析前端并转为json类型
        BufferedReader reader = new BufferedReader(request.getReader());
        StringBuilder json = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            json.append(line);
        }
        JSONObject jsonObject = JSON.parseObject(json.toString());

        //解析前端的参数
        long raceId = Long.parseLong(jsonObject.getString("raceId"));
        System.out.println(raceId);

        Company company = companyService.selectById(raceId);
        //将数据发给前端
        JSONObject message = new JSONObject();
        message.put("company",company);
        response.getWriter().write(message.toString());
    }
}
